package com.cjj.springsecurity.entity;

import com.cjj.common.entity.po.master.CPermission;
import com.cjj.common.entity.po.master.CRole;
import lombok.*;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * @author by BrownC_
 * @date 2021/12/21 10:51:21
 * @email ccc-ju@outlook.com
 */

@Data
@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class User implements Serializable, UserDetails {

    private static final long serialVersionUID = 1L;

    private Long id;

    private String userName;

    private String nickName;

    private String email;

    private String mobile;

    private Date birthday;

    private Integer status;

    private List<CRole> roles;

    private List<CPermission> permissions;

    /**
     * 所有url及method存储的信息
     */
    List<MyGrantedAuthorityUrlMethod> grantedAuthorityUrlMethods;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<SimpleGrantedAuthority> authorities = new ArrayList<>();
        for(CRole role : roles){
            authorities.add(new SimpleGrantedAuthority(role.getRoleNameEn()));
        }
        return authorities;
    }

    @Override
    public String getPassword() {
        return null;
    }

    @Override
    public String getUsername() {
        return userName;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return this.status == 0;
    }
}
